package com.example.teamtribe.main.club.posting;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.database.Posting;
import com.example.teamtribe.database.User;
import com.example.teamtribe.main.club.PostingModel;
import com.example.teamtribe.utils.Utility;

import java.util.ArrayList;
import java.util.List;

public class PostingPresenter implements PostingContract.Presenter {

    private int teamId = -1;
    private PostingContract.View view;
    private PostingModel model;

    public PostingPresenter(PostingContract.View view) {
        this.view = view;
        model = PostingModel.getInstance();
    }

    @Override
    public void start(int postId, List<String> images) {
        getUser(0, postId);
        getImages(images);
        getComments(postId, 0);
    }

    @Override
    public void getUser(final int mode, int postId) {
        model.getHost(Utility.getToken(), postId, new FileModel.FileBack() {
            @Override
            public void loadSuccess(String fileName) {
                if (mode == 0) {
                    view.setHead();
                } else {
                    view.setCommentHead();
                }
            }

            @Override
            public void success(@Nullable Bundle bundle) {
                User user = bundle.getParcelable("user");
                user.saveOrUpdate();
                if (mode == 0) {
                    view.setUser(user);
                } else {
                    view.setCommentUser();
                }
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });
    }

    @Override
    public void getImages(List<String> images) {
        List<String> data = new ArrayList<>();
        for (String string : images) {
            data.add(Utility.getUri(string).toString());
        }
        view.setImage(data);
        for (String string : images) {
            FileModel.getInstance().download(string, 1, new FileModel.FileBack() {
                @Override
                public void loadSuccess(String fileName) {
                    view.setImage();
                }

                @Override
                public void success(@Nullable Bundle bundle) {

                }

                @Override
                public void failure(String meg) {
                    view.showToast(meg);
                }
            });
        }
    }

    @Override
    public void getComments(int postId, int offset) {
        PostingModel.getInstance().getPostings(2, postId, offset, 10, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                List<Posting> postings = bundle.getParcelableArrayList("postings");
                if (postings == null) {
                    postings = new ArrayList<>(0);
                }
                for (Posting posting : postings) {
                    getUser(1, posting.getRealId());
                }
                view.addComment(postings);
                view.complete();
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });
    }

    @Override
    public void comment(int teamId, final String content, final int postId) {
        if (teamId == -1) {
            getTeamId(content, postId);
            return;
        }
        view.showProgress();
        model.post(Utility.getToken(), teamId, null, content, postId, null, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                model.getPosting(bundle.getInt("id"), new Back() {
                    @Override
                    public void success(@Nullable Bundle bundle) {
                        view.addComment((Posting) bundle.getParcelable("posting"));
                        view.clearText();
                        view.hideProgress();
                    }

                    @Override
                    public void failure(String meg) {
                        view.hideProgress();
                        view.showToast(meg);
                    }
                });
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    @Override
    public void delete(int postId) {
        view.showProgress();
        model.deletePost(Utility.getToken(), postId, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.next(PostingActivity.DELETE);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    private void getTeamId(final String content, final int postId) {
        model.getTeamId(postId, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                teamId = bundle.getInt("id");
                if (teamId != 0) {
                    comment(teamId, content, postId);
                }
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });
    }
}
